# coding=utf-8
"""
定时器
"""
from apscheduler.schedulers.background import BackgroundScheduler

from services.datamodel.live.models import mm_Live
from apps.student.share import app_logger
from lib.live.Im import ImMain

from datetime import datetime

class TimedTaskClass:
    """
    定时任务类
    """

    @staticmethod
    def end_live(group_id):
        """
        结束直播
        @param group_id:
        @return:
        """
        live = mm_Live.filter(room_id=group_id).first()
        if int(live.status) == 4:
            mm_Live.update_live_status(3, live_id=live.id)

    @staticmethod
    def send_msg(group_id, command, message, c_id):
        """
        发送自定义消息
        @param group_id:
        @param comment:
        @param message:
        @param c_id: fictitious_comment_id
        @return:
        """
        from services.datamodel.live.models import mm_LiveFictitiousComment
        if mm_LiveFictitiousComment.filter(id=c_id).exists():
            res = ImMain().send_group_msg(group_id, command, message).json()
            app_logger.error(f"完成定时任务:{datetime.now()}，result:{res}，c_id:{c_id}")

    @staticmethod
    def destroy_group(group_id):
        """
        解散直播群
        @param group_id:
        @return:
        """
        res = ImMain().destroy_group(group_id)
        app_logger.error(f"解散直播群:{group_id}，result:{res}")

# 创建定时器
scheduler = BackgroundScheduler()
# 启动定时器
scheduler.start()
